﻿using SBlogCore.Entitys.SysOrgManager;
using SBlogCore.Entitys.SysUserManager;
using SBlogCore.Entitys.SystemManager;

namespace SBlogCore.Entitys.SysRoleManager
{
    public class SysRoleInfo:DeleteEntity, IEntityTypeConfiguration<SysRoleInfo>
    {
        /// <summary>
        /// 名称
        /// </summary>
        [Comment("名称")]
        public string? Name { get; set; }

        /// <summary>
        /// 编码
        /// </summary>
        [Comment("编码")]
        public string? Code { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        [Comment("排序")]
        public int Sort { get; set; } = 1;

        /// <summary>
        /// 备注
        /// </summary>
        [Comment("备注")]
        public string? Remark { get; set; }

        /// <summary>
        /// 分组标记
        /// </summary>
        [Comment("分组标记")]
        public string? RoleGroupCode { get; set; }
        /// <summary>
        /// 多对多（用户）
        /// </summary>
        [Comment("多对多（用户）")]
        public ICollection<SysUserInfo>? SysUsers { get; set; }

        /// <summary>
        /// 多对多中间表（用户角色）
        /// </summary>
        [Comment("多对多中间表（用户角色）")]
        public List<SysUserRole>? SysUserRoles { get; set; }

        /// <summary>
        /// 多对多（机构）
        /// </summary>
        [Comment("多对多（机构）")]
        public ICollection<SysOrgInfo>? SysOrganizations { get; set; }

        /// <summary>
        /// 多对多（菜单）
        /// </summary>
        [Comment("多对多（菜单）")]
        public List<SysMenu>? SysMenus { get; set; }

        /// <summary>
        /// 多对多中间表（角色-菜单）
        /// </summary>
        [Comment("多对多中间表（角色-菜单）")]
        public List<SysRoleMenu>? SysRoleMenus { get; set; }

        public void Configure(EntityTypeBuilder<SysRoleInfo> builder)
        {
            builder.HasMany(p => p.SysMenus)
                .WithMany(p => p.SysRoles)
                .UsingEntity<SysRoleMenu>(
                    u => u.HasOne(c => c.SysMenu).WithMany(c => c.SysRoleMenus).HasForeignKey(c => c.SysMenuId),
                    u => u.HasOne(c => c.SysRole).WithMany(c => c.SysRoleMenus).HasForeignKey(c => c.SysRoleId),
                    u => u.HasKey(c => new { c.SysRoleId, c.SysMenuId })
                );
            builder.HasData(new SysRoleInfo()
            {
                Id = 367366781984837,
                Name = "超级管理员",
                Code = "Admin",
                Sort = 1,
                Remark = "拥有所有的权限,无需单独配置菜单！",
                SysOrgId = 142307070910552,
                CreatedTime = DateTimeOffset.Now,
                CreatedUserId = 142307070910551
            });

        }
    }
}
